package vn.aptech.leaveManagementSystem.view.admin;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import vn.aptech.leaveManagementSystem.business.dto.Holiday;
import vn.aptech.leaveManagementSystem.business.model.Holidays;
import vn.aptech.leaveManagementSystem.business.model.MyModel;
import vn.aptech.leaveManagementSystem.business.parse.MyParse;
import vn.aptech.leaveManagementSystem.dao.DBManager;


import javax.swing.JLabel;
import javax.swing.JTextField;

import com.toedter.calendar.JDateChooser;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PublicHoliday extends JFrame{
	
	DBManager con = new DBManager();
	Connection conn;
	String sql;
	ResultSet rs;
	PreparedStatement ps;
	
	JPanel pnlMain;
	JTable tbHol;
	DefaultTableModel modelHol;
	ArrayList arr_Holiday_id = null;
	private JLabel lblNewLabel;
	private JTextField txtIDholiday;
	private JLabel lblDateFrom;
	private JLabel lblDateTo;
	private JDateChooser txtDateFrom, txtDateTo;
	private JTextField textField_1;
	private JLabel lblDescription;
	private JButton btnAdd;
	private JButton btnUpdate;
	private JButton btnDelete;
	private JButton btnReset;
	JTextArea txtDescription;
	JFrame thisFrame = this;
	

	public PublicHoliday() {
		
		// TODO Auto-generated constructor stub
		
		pnlMain = new JPanel();
		pnlMain.setLayout(null);

		tbHol = new JTable();
		tbHol.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
//				Vector<Object> v = (Vector<Object>) modelApproval.getDataVector().elementAt(tbHol.getSelectedRow());
//				ShowSubForm((int) v.get(0));
				
				int index = tbHol.getSelectedRow(); 
				String stardate = (String) tbHol.getValueAt(index, 0);
		        String IDHoliday = String.valueOf(tbHol.getValueAt(tbHol.getSelectedRow(), 0));
//		        java.sql.Date fromdate = (java.sql.Date)(tbHol.getValueAt(index,1)) ;
//		        java.sql.Date todate = (java.sql.Date)tbHol.getValueAt(index,2);
		        String description = String.valueOf(tbHol.getValueAt(index,3));
		        
		        txtIDholiday.setText(IDHoliday);
//		        txtDateFrom.setDate(fromdate);
//		        txtDateTo.setDate(todate);
		        txtDescription.setText(description); 
			}
		});
		//
		
		//tbHol.setBounds(10, 45, 798, 419);
		JScrollPane spHol=new JScrollPane(tbHol);
		
		spHol.setBounds(10, 11, 798, 240);
		pnlMain.add(spHol);

		getContentPane().add(pnlMain);
		
		lblNewLabel = new JLabel("ID Holiday");
		lblNewLabel.setBounds(67, 279, 65, 14);
		pnlMain.add(lblNewLabel);
		
		txtIDholiday = new JTextField();
		txtIDholiday.setBounds(138, 276, 42, 20);
		txtIDholiday.setEnabled(false);
		pnlMain.add(txtIDholiday);
		txtIDholiday.setColumns(10);
		
		lblDateFrom = new JLabel("Date From :");
		lblDateFrom.setBounds(262, 279, 79, 14);
		pnlMain.add(lblDateFrom);
		
		lblDateTo = new JLabel("Date to :");
		lblDateTo.setBounds(550, 279, 46, 14);
		pnlMain.add(lblDateTo);
		
		txtDateFrom = new JDateChooser();
		txtDateFrom.setBounds(334, 276, 144, 20);
		pnlMain.add(txtDateFrom);
		
		txtDateTo = new JDateChooser();
		txtDateTo.setBounds(606, 276, 144, 20);
		pnlMain.add(txtDateTo);
		
		lblDescription = new JLabel("Description");
		lblDescription.setBounds(67, 311, 93, 14);
		pnlMain.add(lblDescription);
		
		txtDescription = new JTextArea();
		//txtDescription.setBounds(67, 336, 683, 68);
		
		
		
		JScrollPane scrolpane = new JScrollPane(txtDescription);
		scrolpane.setBounds(67, 336, 683, 68);
		pnlMain.add(scrolpane);
		
		btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					// TODO add your handling code here:
					            if (checkLeave())
					            {
					                if (checkDate())
					                {
					                    AddHoliday();
					                }
					            }
					        } catch (SQLException ex) {
					            ex.printStackTrace();
					        } 
			}

			//---Ckeck Null
		      public boolean checkLeave()
		    {
		        String from_date = null;
		        String to_date = null;
		        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");        
		        try{
		            Date fromDate = MyParse.toSqlDate(txtDateFrom.getDate());
		            from_date = sdf.format(fromDate);
		        }
		        catch (NullPointerException ex){
		            JOptionPane.showMessageDialog(null,"You must select from date","Message",JOptionPane.INFORMATION_MESSAGE);
		            return false;
		        }
		        try{
		            Date toDate = MyParse.toSqlDate(txtDateTo.getDate());
		            to_date = sdf.format(toDate);
		        }
		        catch (NullPointerException ex){
		            JOptionPane.showMessageDialog(null,"You must select to date","Message",JOptionPane.INFORMATION_MESSAGE);
		            return false;
		        }
		        if (txtDescription.getText().equals(""))
		        {
		           JOptionPane.showMessageDialog(null,"You must input Description!","Message",JOptionPane.INFORMATION_MESSAGE);
		            return false;
		        }        
		        return true;
		    }
		});
		
		
		
		btnAdd.setBounds(147, 424, 89, 23);
		btnAdd.setIcon(new ImageIcon("icons/gif/16x16/Add.gif"));
		pnlMain.add(btnAdd);
		
		btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO add your handling code here:
				if (checkLeave())
				{
				    if (checkDate())
				    {
				    	updateHoliday();
				    }
				} 
			}


			//---Ckeck Null
		      public boolean checkLeave()
		    {
		        String from_date = null;
		        String to_date = null;
		        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");        
		        try{
		            Date fromDate = MyParse.toSqlDate(txtDateFrom.getDate());
		            from_date = sdf.format(fromDate);
		        }
		        catch (NullPointerException ex){
		            JOptionPane.showMessageDialog(null,"You must select from date","Message",JOptionPane.INFORMATION_MESSAGE);
		            return false;
		        }
		        try{
		            Date toDate = MyParse.toSqlDate(txtDateTo.getDate());
		            to_date = sdf.format(toDate);
		        }
		        catch (NullPointerException ex){
		            JOptionPane.showMessageDialog(null,"You must select to date","Message",JOptionPane.INFORMATION_MESSAGE);
		            return false;
		        }
		        if (txtDescription.getText().equals(""))
		        {
		           JOptionPane.showMessageDialog(null,"You must input Description!","Message",JOptionPane.INFORMATION_MESSAGE);
		            return false;
		        }        
		        return true;
		    }


			private void updateHoliday() {
				// TODO Auto-generated method stub

		        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");        
		        String from_date = null;
		        String to_date = null;
		        Date fromDate = MyParse.toSqlDate(txtDateFrom.getDate());
		        from_date = sdf.format(fromDate);
		        Date toDate = MyParse.toSqlDate(txtDateTo.getDate());
		        to_date = sdf.format(toDate);
		        String description = txtDescription.getText().trim();
		        
		        Holiday hol = new Holiday();
		        hol.setStartDate(fromDate);
		        hol.setEndDate(toDate);
		        hol.setEvent(description);
		        
		       /* Vector v = new Vector();
		        v.add(from_date);
		        v.add(to_date);
		        v.add(description);*/
		        try {
		            MyModel mymodel = new MyModel();
		            mymodel.holidays.updateHoliday(hol);
		            JOptionPane.showMessageDialog(null,"Update successful a Holiday !","Message",JOptionPane.INFORMATION_MESSAGE);
		           displayHoliday();
		        } catch (SQLException er) {
		            System.out.println("Error Add :"+er.getMessage());
		        }
		        
		    
			}
		});
		btnUpdate.setBounds(289, 424, 89, 23);
		btnUpdate.setIcon(new ImageIcon("icons/gif/16x16/Update.gif"));
		pnlMain.add(btnUpdate);
		
		btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try {
					String sql="delete from [Holiday] where IDHoliday = ? ";
					PreparedStatement pst = conn.prepareStatement(sql);
					pst.setInt(1,Integer.parseInt(txtIDholiday.getText()));
					ResultSet rs =pst.executeQuery();
					rs.next();
					rs.deleteRow();
					rs.last();
					} catch (SQLException ex) {
					//Logger.getLogger(ManagerStudent.class.getName()).log(Level.SEVERE, null, ex);
					}
					JOptionPane.showMessageDialog(rootPane, "Delete Sucess"); 
				
				
				/*
				int holiday_id = 0;
		        int [] arrSelected = tbHol.getSelectedRows();       
		        if (arrSelected.length == 0)
		        {
		            JOptionPane.showMessageDialog(null,"You must select a record!","Message",JOptionPane.INFORMATION_MESSAGE);
		        }
		        else
		        {
		            int show = JOptionPane.showConfirmDialog(null,"Are You sure cancel ?");
		            		//(this,"Are You sure cancel ?","Inform",JOptionPane.YES_NO_OPTION);
		            if (show == JOptionPane.YES_OPTION)
		            {
		                for (int i = 0; i < arrSelected.length; i++)
		                {
		                	Holiday hol = new Holiday();
		                    
		                    holiday_id = (Integer)arr_Holiday_id.get(arrSelected[i]);
		                    try {
		                    	MyModel mymodel = new MyModel();
		                        mymodel.holidays.deleteHoliday(holiday_id);
		                        JOptionPane.showMessageDialog(null,"Add successful a Holiday !","Message",JOptionPane.INFORMATION_MESSAGE);
		                        modelHol.setNumRows(0);
		                        displayHoliday();                                           
		                    } catch (SQLException ex) {
		                        ex.printStackTrace();
		                    }                            
		                }		                
		            }     
		        }
			*/}
		});
		btnDelete.setBounds(432, 424, 89, 23);
		btnDelete.setIcon(new ImageIcon("icons/gif/16x16/Delete.gif"));
		pnlMain.add(btnDelete);
		
		btnReset = new JButton("Reset");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtIDholiday.setText("");
				txtDateFrom.setDate(null);
				txtDateTo.setDate(null);
				txtDescription.setText("");
			}
		});
		btnReset.setBounds(586, 424, 89, 23);
		btnReset.setIcon(new ImageIcon("icons/gif/16x16/Redo.gif"));
		pnlMain.add(btnReset);
		
		setVisible(true);
		setTitle("Public Holiday");
		setSize(841, 511);
		setLocationRelativeTo(null);
		
		try {
			displayHoliday();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public void displayHoliday() throws SQLException
    {
		arr_Holiday_id = new ArrayList();
	modelHol = new DefaultTableModel();
	modelHol.addColumn("ID");
	modelHol.addColumn("Start Date");
	modelHol.addColumn("E Date");
	modelHol.addColumn("Event Name");
//	Holiday hol = new Holiday();
//	int idHol = hol.getIDHoliday();
	try {
		MyModel mymodel = new MyModel();
		ResultSet rs = mymodel.holidays.getHolidayList(); // rs=rsEmployee
		while (rs.next()) {
			
			String[] strThol=new String[4];
			strThol[0]=new Integer(rs.getInt("IDHoliday")).toString();
			strThol[1]=rs.getDate("StartDate").toString();
			strThol[2]=rs.getDate("EndDate").toString();
			strThol[3]=rs.getString("Event");
			System.out.println(strThol[1]);
			modelHol.addRow(strThol);															
		}
		tbHol.setModel(modelHol);
	 
	} catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	
    }
	
	//----public Add Holiday
    public void AddHoliday()  throws SQLException
    {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");        
        String from_date = null;
        String to_date = null;
        Date fromDate = MyParse.toSqlDate(txtDateFrom.getDate());
        from_date = sdf.format(fromDate);
        Date toDate = MyParse.toSqlDate(txtDateTo.getDate());
        to_date = sdf.format(toDate);
        String description = txtDescription.getText().trim();
        
        Holiday hol = new Holiday();
        hol.setStartDate(fromDate);
        hol.setEndDate(toDate);
        hol.setEvent(description);
        
       /* Vector v = new Vector();
        v.add(from_date);
        v.add(to_date);
        v.add(description);*/
        try {
            MyModel mymodel = new MyModel();
            mymodel.holidays.addHoliday(hol);
            JOptionPane.showMessageDialog(null,"Add successful a Holiday !","Message",JOptionPane.INFORMATION_MESSAGE);
           displayHoliday();
        } catch (SQLException er) {
            System.out.println("Error Add :"+er.getMessage());
        }
        
    }
    //----End Add Holiday

	//this method used to check date
    public boolean checkDate()
    {
        String from_date = null;
        String to_date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        Date fromDate = MyParse.toSqlDate(txtDateFrom.getDate());
        //from_date = sdf.format(fromDate);
        Date toDate = MyParse.toSqlDate(txtDateTo.getDate());
        //to_date = sdf.format(toDate);
        int k = fromDate.compareTo(toDate);
        if (k > 0 || k == 0)
        {
            JOptionPane.showMessageDialog(null,"Invalid date! (From date < To date)","Message",JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
        return true;
    }
}
